﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace PlaystationNetworkAPI.Core.US
{
    internal class ProfileParser : Common.Parser
    {
        /// <summary>
        /// TrophiesParser constructor.
        /// </summary>
        /// <param name="htmlCode">HTML code to be handled</param>
        internal ProfileParser(string htmlCode) : base(htmlCode) { }

        /// <summary>
        /// Get PSN ID Level
        /// </summary>
        /// <returns>Level number</returns>
        private int? GetLevel()
        {
            return TryParseNullableInt(GetInnerText("div", "id", "leveltext", null, _HtmlCode));
        }

        /// <summary>
        /// Get the PSN ID progress to the next level.
        /// </summary>
        /// <returns>Progress percentage number.</returns>
        private int? GetProgress()
        {
            return TryParseNullableInt(GetInnerText("div", "class", "progresstext", "%", _HtmlCode));
        }

        /// <summary>
        /// Get the total number of bronze trophies.
        /// </summary>
        /// <returns>The bronze trophies number.</returns>
        private int? GetBronzeTrohies()
        {
            return TryParseNullableInt(GetInnerText("div", "class", "text bronze", "Bronze", _HtmlCode));
        }

        /// <summary>
        /// Get the total number of silver trophies.
        /// </summary>
        /// <returns>The silver trophies number.</returns>
        private int? GetSilverTrohies()
        {
            return TryParseNullableInt(GetInnerText("div", "class", "text silver", "Silver", _HtmlCode));
        }

        /// <summary>
        /// Get the total number of gold trophies.
        /// </summary>
        /// <returns>The gold trophies number.</returns>
        private int? GetGoldTrophies()
        {
            return TryParseNullableInt(GetInnerText("div", "class", "text gold", "Gold", _HtmlCode));
        }

        /// <summary>
        /// Get the total number of platinum trophies.
        /// </summary>
        /// <returns>The platinum trophies number.</returns>
        private int? GetPlatinumTrophies()
        {
            return TryParseNullableInt(GetInnerText("div", "class", "text platinum", "Platinum", _HtmlCode));
        }

        /// <summary>
        /// Get the total number of trophies.
        /// </summary>
        /// <returns>The total trophies number.</returns>
        private int? GetTotalTrophies()
        {
            return TryParseNullableInt(GetInnerText("div", "id", "text", null, _HtmlCode));
        }

        /// <summary>
        /// Get the PSN ID from the source code.
        /// </summary>
        /// <returns>PSN ID from the source code.</returns>
        private string GetPsnId()
        {
            return GetInnerText("div", "id", "id-handle", null, _HtmlCode);
        }

        /// <summary>
        /// Get the Small Avatar path.
        /// </summary>
        /// <returns>Small Avatar path.</returns>
        private string GetAvatarSmall()
        {
            return GetImgSource("avatar_s", _HtmlCode);
        }

        /// <summary>
        /// Get the Playstation Network Profile from the source page.
        /// </summary>
        /// <returns>Playstation Network Profile</returns>
        internal Profile GetProfile()
        {
            TrophyCount collectedTrophyCount = new TrophyCount()
            {
                Bronze = GetBronzeTrohies(),
                Silver = GetSilverTrohies(),
                Gold = GetGoldTrophies(),
                Platinum = GetPlatinumTrophies(),
                Total = GetTotalTrophies()
            };

            Profile collectedProfile = new Profile()
            {
                Location = "US",
                PsnId = GetPsnId(),
                AvatarSmall = GetAvatarSmall(),
                Level = GetLevel(),
                Progress = GetProgress(),
                TrophyCount = collectedTrophyCount,
            };

            return collectedProfile;
        }
    }
}
